<template>
  <div class="custom_video_list">
    <div class="custom_video_list_content" v-if="listData.length > 0">
      <div
        class="custom_video_list_content_item"
        v-for="(item, index) in listData"
        :key="'videoList_index_' + index"
      >
        <VideoListItem :item="item"></VideoListItem>
      </div>
    </div>
    <div v-else class="custom_video_list_content nodata">
      <span>{{ $t("lang.暂无数据") }}</span>
    </div>
    <div
      class="loadmore"
      v-if="!isDownLoadMore && !isLoading && listData.length > 0"
    >
      <a href="javascript:;" @click="loadMoreClickHandle">{{
        $t("lang.点击加载更多")
      }}</a>
    </div>
    <div class="loading" v-if="isLoading">
      <van-loading size="24px">{{ $t("lang.加载中") }}</van-loading>
    </div>
  </div>
</template>
<script>
export default {
  name: "videoList",
  props: {
    item: {
      type: Object,
      default() {
        return {};
      },
    },
    isDownLoadMore: {
      type: Boolean,
      default() {
        return true;
      },
    },
  },
  components: {
    VideoListItem: () =>
      import(
        /* webpacChunkName:'videoListItem' */ "../../components/videoList/videoListItem"
      ),
  },
  data() {
    return {
      isLoading: false,
      listData: [],
    };
  },
  created() {
    this.requestData();
  },
  mounted() {
    let that = this;
    if (this.$props.isDownLoadMore) {
      this.$query(window).on("scroll", function(e) {
        that.scrollHandle(e);
      });
    }
  },
  methods: {
    scrollHandle(e) {
      let that = this;
      let top = e.target.documentElement.scrollTop,
        height = screen.height,
        scrollHeight = e.target.documentElement.scrollHeight;
      that.videoListTimeout && clearTimeout(that.videoListTimeout);
      that.videoListTimeout = setTimeout(() => {
        if (top + height + 60 >= scrollHeight) {
          that.requestData(that.$props.item, true);
        }
      }, 500);
    },
    loadMoreClickHandle() {
      this.requestData(this.$props.item, true);
    },
    requestData(bool) {
      if (!this.isLoading) {
        this.isLoading = true;
        this.listData = bool
          ? this.listData.concat(this.$props.item.data)
          : this.$props.item.data;
        this.isLoading = false;
      }
    },
  },
};
</script>
<style lang="less">
@base: 23.44rem;
.custom_video_list {
  .custom_video_list_content {
    display: block;
    overflow: hidden;
    &.nodata {
      span {
        display: block;
        text-align: center;
        font-size: (14 / @base);
        margin: 0 auto;
        line-height: (100 / @base);
      }
    }
    .custom_video_list_content_item {
      float: left;
      width: 49.6%;
      margin: (1 / @base) (0.5 / @base) 0 (0.5 / @base);
    }
  }
  .loading {
    padding: (10 / @base) 0 (20 / @base) 0;
  }
  .loadmore {
    font-size: (14 / @base);
    margin: (5 / @base);
    text-align: center;
    /*     background-color: #fff;
    box-shadow: 0 2px 12px rgba(100, 101, 102, 0.12);
 */
    border-radius: (2.5 / @base);
    a {
      padding: (10 / @base);
      color: #999;
      display: block;
    }
  }
}
</style>
